package org.artifact;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.log.Log;
import cn.hutool.log.LogFactory;
import org.artifact.xls.ExcelExport;
import org.yaml.snakeyaml.Yaml;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.HashMap;
import java.util.Map;

public class ExportTable {
    private static final Log log = LogFactory.get(ExportTable.class);

    public static void main(String[] args) throws Exception {
        log.info("**** 开始导出 **** ");

        String userDir = System.getProperty("user.dir");
//        userDir = "D:\\workSvn\\Public\\配置表\\Config\\";
        String exportConfigPath = userDir + File.separator + "ExportConfig.yml";
        File exportConfig = FileUtil.file(exportConfigPath);
        if (!FileUtil.exist(exportConfig)) {
            log.error("**** 没有找到导出配置文件{} **** ", "ExportConfig");
        }


        // 生成客户端用的json
        Yaml yaml = new Yaml();
        Map config = null;
        try {
            config = (Map) yaml.load(new FileInputStream(exportConfig));
        } catch (FileNotFoundException e) {
            log.error("**** 导出配置文件异常 **** ");
        }
        // excle路径
        String excelPath = userDir + File.separator + MapUtil.getStr(config, "ExcelPath");
        // 输出路径
        String outputPath = userDir + File.separator + MapUtil.getStr(config, "OutPutPath");
        // java包名称
        String javaPackageName = MapUtil.getStr(config, "JavaPackageName");
        // 继承关系
        Map<String, String> _exts = MapUtil.get(config, "extends", Map.class);

        Map exts = new HashMap();
        if (_exts!=null){
            for (Map.Entry<String, String> entry : _exts.entrySet()) {
                // 首字母转大写
                exts.put(StrUtil.upperFirst(entry.getKey()), StrUtil.upperFirst(entry.getValue()));
            }
        }

        ExcelExport.builder()
                .designSourcePath(excelPath)
                .outputPath(outputPath)
                .packageName(javaPackageName)
                .exts(exts)
                .build().exec();
        log.info("**** 导出完成 **** ");
    }
}
